package cn.josh.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginFilter implements Filter
{


	public void destroy() 
	{
		
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException 
	{
		HttpServletRequest http_request = (HttpServletRequest)request;
		HttpServletResponse http_response = (HttpServletResponse)response;
		
		//得到session域中的用户名
		HttpSession session = http_request.getSession();
		String userName = (String)session.getAttribute("userName");
		
		String path = http_request.getRequestURI();
		
		if(userName == null)
		{
			//需要过滤的界面
			if(path.indexOf("/sp_cart.jsp") != -1 || 
			   path.indexOf("/order_item.jsp") != -1 ||
			   path.indexOf("/order.jsp") != -1 ||
			   path.indexOf("/ArticleCarWriteServlet") != -1 ||
			   path.indexOf("/ArticleCarReadServlet") != -1 ||
			   path.indexOf("/OrderReadServlet") != -1 ||
			   path.indexOf("/OrderSubmitServlet") != -1) 
			{
				http_response.sendRedirect("/ShoppingWebsite/jsp/login.jsp");
				return;
			}
		}

		chain.doFilter(http_request, http_response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException 
	{
		System.out.println("loginfilter创建了");
		
	}

}
